<style>
.shop-name{color:red;font-weight:bold;}
.newshop-name{color:red;font-weight:bold;}
.area-name{font-weight:bold;font-size:12px;padding-top:4px;padding-right:4px;padding-bottom:4px;}
.weight_rule li{float:left;padding:4px;text-align:left;}
.weight_rule ul{width:600px;}
.weight_left{width:200px;}
 .weight_right{width:380px;}
select{width:80px;}
ul{padding:4px;}
</style>

规则设置详情<br>
<div class="tableform">
<form id='rule_form' method="post" action="index.php?app=logistics&ctl=admin_area_rule&act=editArearule">

<div class="division">
<table width="100%" cellspacing="0" cellpadding="0" >
    <tbody>
    <tr>
        <th>规则名称:</th>
        <td><{input type="text" name="rule_name" value=$rule.rule_name vtype="required"}></td>
    </tr>

    <tr>
        <th>适用地区:</th>
        <td>
            <table width="100%" cellspacing="1" cellpadding="1" style="border: 1px solid #cccccc; ">
            <{foreach from=$area item=area}>
            <tr style="border: 1px solid #cccccc; ">
                <td ><{$area.local_name}></td>
                <td style="border: 1px solid #cccccc; "><input type="checkbox" onclick="showAll(this,<{$area.area_id}>)">全选</td>
                <td style="border: 1px solid #cccccc; ">
                    <div style="width:450px;">
                    <{foreach from=$area.area_items item=area_items}> 
                        <li style="width:80px;float:left">
                        <input class="item<{$area.area_id}> shop-name" type="checkbox" name="first_city[]" value="<{$area_items.region_id}>" <{if in_array($area_items.region_id,$rule_list.class_city)}>checked<{/if}> onclick="javascript:if(this.checked){this.getNext('span').addClass('newshop-name');}else{this.getNext('span').removeClass('newshop-name');this.getNext('span').removeClass('shop-name');}">
                        <span style="padding:8px;" id='<{$area_items.region_id}>' <{if in_array($area_items.region_id,$rule_list.class_city)}>class="shop-name shops-name"<{/if}> ><{$area_items.local_name}></span>
                    <{/foreach}>
                    </div>
                </td>
            </tr>
            <{/foreach}>
            </table>
        </td>
    </tr>
    <tr>
      <th>绑定店铺:<{help}>不选择，默认为全部<{/help}></th>
      <td colspan="2">
        <{foreach from=$shopData item=shop}>
        <label><input type="checkbox" name="item[]" <{if in_array($shop.shop_id , $rule['shop_id_array']) }>checked="checked"<{/if}> value="<{$shop.shop_id}>"><span><{$shop.name}></span></label>
        <{/foreach}>
      </td>
    </tr>
    <tr>
      <th>绑定物料:<{help}>不选择，默认为全部<{/help}></th>
      <td colspan="2">

        <{button label="物料库频道" id="material-find-btn" }>&nbsp;&nbsp;<{button type="button" id="material-delall-btn" label="全部删除" }>
        <input type="hidden" id="material_sales" name="material_sales_bn" value="<{$rule['material_bn']}>">
        <table class="gridlist" id="material_table"  style="margin:4px 0;width:100%;">
          <thead>
          <tr>
            <th>编码</th>
            <th style="">名称</th>
            <th style="display: none">数量</th>
            <th style="display: none">促销价格贡献占比(1-100整数数值)</th>
            <th style="width:30px;">删除</th>
          </tr>
          </thead>
          <tbody id="dataNode">
          <tr>
            <td colspan="7" style="padding:0;"><div class="note" style="margin:0;"> 暂无物料信息 </div></td>
          </tr>
          </tbody>
        </table>
      </td>
    </tr>
    <tr>
      <th>规则权重:</th>
      <td colspan="2">
        <input autocomplete="off" class="x-input " type="text"  name="priority" size="2"  vtype="number" value="<{$rule.priority}>">
      </td>
    </tr>
    <tr>
      <th>备注:</th>
      <td colspan="2">
        <input autocomplete="off" class="x-input " type="text"  name="memo" size="200"  value="<{$rule.memo}>">
      </td>
    </tr>
    <tr>
        <th>规则类型:</th>
        <td colspan="2">
        <input type='radio' id='noweighttype' class='first_rule_weight' name='set_type' value='noweight' <{if $rule.set_type=='noweight'}> checked<{/if}> onclick="showArea(this.value)">按任意重量设置
        <input type='radio' id='weighttype' class='first_rule_weight' name='set_type' value='weight' <{if $rule.set_type=='weight'}> checked<{/if}> onclick="showArea(this.value)">按重量区间设置
        </td>
    </tr>

    <tr>
        <th>规则详情:</th>
        <td colspan="2">
            <div  id='first_rule_weight_one' style="display:none;padding:2px;">
                区间类型
                <span class="spectype"><input type="radio" name="addspectype" class="addspectype" value="weight" checked >普通区间</span>
                <span class="spectype"><input type="radio" name="addspectype" value="above" class="addspectype">"以上"区间</span>
                <input type="button" id ='addspec' value='添加'>
                <{help}>重量请输入整数,<br>请输入连续的区间,重量区间表示:左区间<=重量<右区间<{/help}><br>
                <{if $rule.set_type=='weight'}>
                <{foreach from=$rule.item_list item=item_list}>
                <div class='weight_rule' id="<{$item_list.item_id}>">
                    <ul>
                        <li class="weight_left">
                            重量:<input type="text"  name="min_weight[]" size=6 value="<{$item_list.min_weight}>" vtype="required&&unsigned">g&nbsp;&nbsp;&nbsp;
                            <{if $item_list.max_weight=='-1'}>&nbsp;&nbsp以上
                                <input type="hidden" name="max_weight[]" size=6 value="-1" >
                            <{else}>
                                至<input type="text" name="max_weight[]" size=6 value="<{$item_list.max_weight}>" vtype="required&&unsigned">g
                            <{/if}>
                        </li>
                        <li class="weight_right">
                            首选物流公司:<select name="corp_id[]" id="dlyCorpWeight<{$item_list.item_id}>" onchange="dlyCorpChange('Weight<{$item_list.item_id}>')">
                            <{foreach from=$dly_corp item=dlyCorp1}>
                                <option value='<{$dlyCorp1.corp_id}>' <{if $item_list.corp_id==$dlyCorp1.corp_id}>selected<{/if}>><{$dlyCorp1.name}></option>
                            <{/foreach}>
                            </select>
                            <span id="secondSpanWeight<{$item_list.item_id}>" <{if !in_array($item_list.corp_id,$elecIds)}>style="display:none;"<{/if}>>
                            次选物流公司:<select name='second_corp_id[]' id="secondDlyCorpWeight<{$item_list.item_id}>">
                                <option value='0'>--请选择--</option>
                            <{foreach from=$dlyCorpNormal item=dlyCorp2}>
                                <option value='<{$dlyCorp2.corp_id}>' <{if $item_list.second_corp_id==$dlyCorp2.corp_id}>selected<{/if}>><{$dlyCorp2.name}></option>
                            <{/foreach}>
                            </select>
                            </span>
                            <a onclick='deleteArea($(this).getParent("div"))'>删除</a>
                        </li>
                    </ul>
                </div>
                <input type="hidden" name="item_id[]" value="<{$item_list.item_id}>">
                <{/foreach}>
                <{/if}>
                <div id="spec_body"></div>
            </div>

            <div  id='first_rule_weight_nothing' style="display:none">
                首选物流公司:<select name='default_corp_id' id='dlyCorpNoweight' onchange="dlyCorpChange('Noweight')">
                <{foreach from=$dly_corp item=dlyCorp1}>
                    <option value='<{$dlyCorp1.corp_id}>' <{if $rule.item_list.0.corp_id==$dlyCorp1.corp_id}>selected<{/if}>><{$dlyCorp1.name}></option>
                <{/foreach}>
                </select>
                <span id="secondSpanNoweight" <{if !in_array($rule.item_list.0.corp_id,$elecIds)}>style="display:none;"<{/if}>>
                次选物流公司:<select name='default_second_corp_id' id='secondDlyCorpNoweight'>
                    <option value='0'>--请选择--</option>
                <{foreach from=$dlyCorpNormal item=dlyCorp2}>
                    <option value='<{$dlyCorp2.corp_id}>' <{if $rule.item_list.0.second_corp_id==$dlyCorp2.corp_id}>selected<{/if}>><{$dlyCorp2.name}></option>
                <{/foreach}>
                </select>
                </span>
                <input type="hidden" name="default_item_id" value="<{$rule.item_list.0.item_id}>">
            </div>
        </td>
    </tr>
    </tbody>
</table>
</div>

<div class="table-action">
    <{button type="button" label="保存" id="edit_rule" }>
</div>
<input type="hidden" name="rule_id" value="<{$rule_id}>">
<input type="hidden" name="branch_id" value="<{$rule.branch_id}>">
<input type='hidden' name='deleteareaflag' id='deleteareaflag' value='0'>
<input type="hidden" id='relationflag' name="relationflag" value='0'>
</form>
</div>

<script>
  /**
   * JS 加载到顶部
   * @param {String} url
   * @param {Boolean} 请求并加载
   * @param {Function} fn
   * @return {Boolean}
   */
  function requireJs(url, multi, fn) {
    if (typeof multi == 'function') {
      var _temp = fn;
      fn = multi;
      multi = _temp;
    }
    var ss = document.getElementsByTagName('script');
    var loaded = false;
    for (var i = 0; i < ss.length; i++) {
      if (ss[i].src && ss[i].getAttribute('src') == url) {
        loaded = true;
        break;
      }
    }
    if (loaded) {
      if (fn && typeof fn != 'undefined' && fn instanceof Function){
        fn();
        return false;
      }
    }
    var s = document.createElement('script'),
        b = false;
    s.setAttribute('type', 'text/javascript');
    if (multi) {
      this.loadJs = fn;
      var noArg = url.indexOf('?') == -1;
      s.setAttribute('src', url + (noArg?'?':'&') + 'callback=WYSIWYG.loadJs');
    } else {
      s.setAttribute('src', url);
      s.onload = s.onreadystatechange = function () {
        if (!b && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
          b = true;
          if (fn && typeof fn != 'undefined' && fn instanceof Function) fn();
        }
      };
    }
    document.getElementsByTagName('head')[0].appendChild(s);
  }

  var js_path='js';
    var res_url='/app/desktop/statics/';
      var	hs = {
            'cmdrunner':{path:res_url+js_path+'/coms/cmdrunner.js',type:'js'},
            'historyMan':{path:res_url+js_path+'/coms/hst.js',type:'js'},
            'autocompleter':{path:res_url+js_path+'/coms/autocompleter.js',type:'js'},
            'uploader':{path:res_url+js_path+'/coms/uploader.js',type:'js'},
            'modedialog':{path:res_url+js_path+'/coms/modedialog.js',type:'js'},
            'pager':{path:res_url+js_path+'/coms/pager.js',type:'js'},
          'colorpicker':{path:res_url+js_path+'/coms/colorpicker.js',type:'js'},
      'editor_style_1':{path:res_url+js_path+'/coms/editor_style_1.js',type:'js',requires:['picker']},
        'picker':{path:res_url+js_path+'/coms/datapicker.js',type:'js',requires:['colorpicker']},
        'editor':{path:res_url+js_path+'/coms/editor.js',type:'js',requires:['editor_style_1']}
      };

      Object.each(hs,function(v,k){
//        Ex_Loader.add(k,v);
//        console.log(v.path);
        requireJs(v.path);
      });

      if(window.ie6)Ex_Loader('<{$env.app.res_url}>/js/fixie6.js');


var corp_id=<{$dly_corp_list}>;
var dlyCorpNormal = <{$dlyCorpNormalJson}>;
var electronIds = <{$electronIds}>;

function showAll(area,area_id){
    if(area.checked==true){
        $$('.item'+area_id).each(function(i,index){
            if(!i.get('disabled')){
                i.set('checked',true);
                i.getNext('span').addClass('shop-name');
            }
        });
    } else {
        $$('.item'+area_id).each(function(i,index){
            i.set('checked',false);
            i.getNext('span').removeClass('shop-name');
        });
    }
}

function deleteArea(d){
    if (!confirm('删除后无法恢复，确定要删除吗？')){
        return;
    }

    d.remove();
 
    if(d.get('id')){
       new Request({url:'index.php?app=logistics&ctl=admin_area_rule&act=deleteRule&item_id='+d.get('id'),
        onComplete:function(rs){
            var json = Json.evaluate(rs);
            if (typeof(json.error) != 'undefined'){
                alert('删除失败');
            }else{
                alert('删除成功');
            }
        }}).send();
    }
}

$('addspec').addEvent('click',function(e){
    var add_above=0;
    $$('.weight_left').each(function(etd){
        above_max_weight = etd.getChildren('input[name^=max_weight]').get('value')/1;
        if(above_max_weight=='-1'){
            add_above++;
            
        }
    });

    if(add_above>=1){
        return MessageBox.error('<{t}>以上区间存在不可以再添加!<{/t}>');
    }else{
        var spectype = $('first_rule_weight_one').getElement("input[name='addspectype']:checked").value;
        var index = uniqueID();
        var newRow=new Element('div[class="areabox"]',{html:getTemp(spectype,index)}).inject($('spec_body'));
    }
});

var getTemp = function(type,index){
    temp='<div class="weight_rule"><ul><li class="weight_left">重量:<input type="text"  name="min_weight[]" size=6 vtype="required&&unsigned">g&nbsp;&nbsp;&nbsp;&nbsp;';
    if(type=='weight'){
        temp+='至<input type="text" name="max_weight[]" size=6 vtype="required&&unsigned">g';
    }else{
        temp+='&nbsp;&nbsp以上<input type="hidden" name="max_weight[]" value="-1">';
    }

    temp+='</li><li class="weight_right">首选物流公司:';
    temp+='<select name="corp_id[]" id="dlyCorp'+index+'" onchange="dlyCorpChange('+index+')">';
    temp+='<option value="0">--请选择--</option>';
    if(corp_id){
        corp_id.each(function(i,index){
            temp+='<option value='+i.corp_id+'>'+i.name+'</option>';
        });
    }
    temp+="</select>";
    temp+='<span id="secondSpan'+index+'" style="display:none;">次选物流公司:<select name="second_corp_id[]" id="secondDlyCorp'+index+'">';
    temp+='<option value="0">--请选择--</option>';
    if(dlyCorpNormal){
        dlyCorpNormal.each(function(i,index){
            temp+='<option value='+i.corp_id+'>'+i.name+'</option>';
        });
    }
    temp+="</select></span>";
    temp+="<a onclick='deleteArea($(this).getParent(\"div\"))'>删除</a>";
    temp+="</li></ul></div>";

    return temp;
};

function dlyCorpChange(index) {
    var dlyCorp = 'dlyCorp'+index;
    var secondDlyCorp = 'secondDlyCorp'+index;
    var secondSpan = 'secondSpan'+index;

    var val = $(dlyCorp).value;
    if(electronIds.contains(val)) {
        $(secondSpan).show();
    } else {
        $(secondSpan).hide();
    }
    $(secondDlyCorp).selectedIndex = 0;
}

function showArea(setType){
    if(setType=='weight'){
        $('first_rule_weight_one').setStyle('display','');
        $('first_rule_weight_nothing').setStyle('display','none');
    }
    if(setType=='noweight'){
        $('first_rule_weight_nothing').setStyle('display','');
        $('first_rule_weight_one').setStyle('display','none');
    }
}
showArea('<{$rule.set_type}>');

$$('.first_rule_weight').addEvent('click',function(i,index){
    confirmchange = confirm('切换规则类型,现有类型将会全部删除?');
    if(confirmchange==false){
        if(this.value=='weight'){
            showArea('noweight');
            this.set('checked',false);
            $('noweighttype').set('checked',true);
        }else{
            this.set('checked',false);
            $('weighttype').set('checked',true);
            showArea('weight');
        }
    }
});

$('edit_rule').addEvent('click',function(e){
    var _this=this;
    var form=this.getParent('form');
    var class_city_id = '<{$rule_list.class_city_id}>';
    var set_type=$('rule_form').getElements("input[name='set_type']:checked").get('value');

    if($$("input[name^=first_city]:checked").length<=0){
        return MessageBox.error('<{t}>请为此条规则选择区域!<{/t}>');
    }

    if($('rule_form').getElements("input[name='set_type']:checked").length <=0){
        return MessageBox.error('<{t}>请选择规则类型!<{/t}>');
    }

    if(set_type=='weight'){
        if($$('.weight_left').length<=0){
            return MessageBox.error('<{t}>请选择对应的重量区间!<{/t}>');
        }

        var above_set=0;
        compareValueFlag = $$('.weight_left').every(function(etd){
            min_weight = etd.getChildren('input[name^=min_weight]').get('value')/1;
            max_weight = etd.getChildren('input[name^=max_weight]').get('value')/1;

            if(max_weight=='-1'){
                above_set++;
            }
            if((min_weight>max_weight || min_weight==max_weight) && max_weight!='-1'){
                return false;
            }
            return true;
        });

        if( !compareValueFlag )return MessageBox.error('<{t}>重量值为空或者最小值不可以大于最大值<{/t}>');
        if(above_set>=2) return MessageBox.error('<{t}>以上区间只能有一个<{/t}>');

        //区间判断重复
        var length=$$('.weight_left').length;

        for(i=0;i<length;i++){
            imin = $$('.weight_left')[i].getChildren('input[name^=min_weight]').get('value')/1;
            imax = $$('.weight_left')[i].getChildren('input[name^=max_weight]').get('value');

            for(j=i+1;j<length;j++){
                min=$$('.weight_left')[j].getChildren('input[name^=min_weight]').get('value')/1;
                max=$$('.weight_left')[j].getChildren('input[name^=max_weight]').get('value')/1;
                if(imax=='-1'){
                    if(min>imin/1){
                        return MessageBox.error(min+'大于'+imin+',<{t}>大于以上区间重量最小值<{/t}>');
                    }
                }
                if(imax>min){
                    return MessageBox.error(imax+'大于'+min+'<{t}>重复区间存在<{/t}>');
                }
            }
        }
    }

    var deleteArea=[];
    var newaddArea=[];
    $$('.newshop-name').each(function(e){
        newaddArea.push(e.getPrevious('input[name^=first_city]').get('value'));
    });

    if(newaddArea.length>0){
        newaddArea=newaddArea.join(",");
    }

    var ckdeletearea = $$('.shops-name').every(function(e){
        if(e.getPrevious('input[name^=first_city]').get('checked')==false){
            deleteArea.push(e.getPrevious('input[name^=first_city]').get('value'));
            return false;
        }
        return true;
    });

    if(!ckdeletearea){
        var url = 'index.php?app=logistics&ctl=admin_rule&act=confirmDeleteRule';
        new Dialog(url,{title:'确认',width:500,height:400});
    }else{
        if(newaddArea){
            var relationRule='';
            new Request({
                url:'index.php?app=logistics&ctl=admin_area_rule&act=areaFilter&oper=ckarea&branch_id=<{$rule.branch_id}>&p_region_id='+newaddArea,async:false,
                onComplete:function(rs){
                    if(rs){
                        rs=JSON.decode(rs);

                        rs.each(function(i,index){
                            relationRule+='['+i.region_name+']';
                            relationRule+='\n';
                        });
                    }
                }
            }).send();
        }

        if(relationRule!=''){
            var relationflag = confirm('系统检测到\n'+relationRule+'\n 有下级地区规则存在,是否带出?');
            if(relationflag==false){
                $('relationflag').set('value',1);
            }
        }

        form.store('target',{
            onRequest:function(){
                _this.disabled=true;
            },
            onComplete:function(jsontext){
                try{
                    var json = Json.evaluate(jsontext);
                    if (typeof(json.error) != 'undefined'){
                        _this.disabled=false;
                    }else{
                        _this.disabled=true;
                        _this.getParent('.dialog').retrieve('instance').close();
                        MessageBox.success('<{t}>保存成功<{/t}>');
                    }
                }catch(e){}
            }
        });
        form.fireEvent('submit',e);
    }
});

uniqueID = (function () {
    var id = 0;
    return function () {
        return id++;
    };
})();


(function() {
  window.addEvent('domready', function() {
    var material_sales = $('material_sales').value;
    init_material(material_sales);

    var callurl='index.php?app=material&ctl=admin_material_basic&act=getMaterial',store=[];

    var tpl='<tr key="{bm_id}" id="bm_{bm_id}" title="点击选取/反选此条记录">'
        +'  <td>{material_bn}</td><td class="material-name">{material_name}</td>'
        +'  <td style="display: none"><input type="text" value="{num}" key="num" vtype="number&amp;&amp;required" tname="at[_PRIMARY_]" size="6"></td>'
        +'  <td style="display: none"><input type="text" vtype="number&amp;&amp;required"  tname="pr[_PRIMARY_]" key="rate" value="{rate}" size="5"></td>'
        +'  <td><{img src="bundle/delecate.gif" app="desktop" key="state" class="pointer btn-delete-item"}></td>'
        +'</tr>';

    $('material-find-btn').addEvent('click',function(e){
      var url='index.php?app=desktop&act=alertpages&goto='+encodeURIComponent('index.php?app=material&ctl=admin_material_basic&act=findMaterial');
      new finderDialog(url,{params:{url:callurl,name:'bm_id[]'},width:1000,height:660,
        onCallback:function(rs){
          if(!rs)return;
          rs=JSON.decode(rs);
          init(rs);
        }
      });
    });

    var pag,rows;
    function emptyData(){
      $('material_sales').value = '';

      var noData='<tr>'
          +'<td colspan="8" style="padding:0;"><div class="note" style="margin:0;"> 暂无物料信息 </div></td>'
          +'</tr>';
      $('dataNode').set('html',noData);
    }

    function createProduct(data){
      var material_sales;
      var materialbn = '';
      for(var i=0;i<data.length;i++){
        materialbn += data[i].material_bn + ",";
      }
      $('material_sales').value = materialbn;

      pag = new PageData(tpl,data,{'updateMain':$('dataNode'),'pageNum':5,PRIMARY_ID:'bm_id',
        'onShow':function(){
          var _this=this;
          $$('#dataNode input[type]').addEvent('change',function(e){
            var pid=this.getParent('tr').get('key'),value=this.value;

            _this.editData(pid,[this.get('key'),value]);
          });

          rows=$ES('#dataNode tr');
          rows.each(function(item,i){
            item.addEvent('click',function(e){
              this.toggleClass('selected');
            });
            item.getElement('.btn-delete-item').addEvent('click',function(e){
              if(_this.selectData(item.get('key')) && confirm('确定要删除 '+ _this.selectData(item.get('key'))['material_name'] +' 吗？')){
                _this.delData(item.get('key'));
                //删除material_sales中对应的值
                var materialbn_current = $('material_sales').value;
                var materialbn_del = item.getElements("td")[0].get('html');

                $('material_sales').value =  materialbn_current.substring(0,materialbn_current.lastIndexOf(materialbn_del))+
                materialbn_current.substring(materialbn_current.lastIndexOf(materialbn_del)+materialbn_del.length+1,materialbn_current.length);
              }
              if(!$E('#dataNode tr')) emptyData();
            });
            item.getElement('input[tname^=at]').addEvent('keypress',function(e){
              if(e.code==13) $E('#pfba input').focus();
            });
          });

          if(this.data.length) rows[0].getElement('input[key^=num]').focus();

          $ES('.material-name').removeEvent('mouseover').addEvent('mouseover',function(e){
            if (this.get('visibility')=='false')
            {
              var e  = new Event(e), el = e.target;
              visiTips.attach(el);
              el.addEvent('mouseleave',function(){
                this.removeClass('active');
              });
              el.fireEvent('mouseenter',e);
            }
          });
        }
      });
    }

    function init_material(material_bn){
      console.log(material_bn);
      console.log(material_bn.split(","));
      var material_array = material_bn.split(",");
      var data_array = new Array();
      var data_string = '';
      material_array.each(function(value, key){
        if(value != ''){
          //material_array[key] = 'material_bn[]=' + value;
          data_array.push('material_bn[]=' + value);
        }
      });

      data_string = data_array.join("&");
      var req = new Request({
        url:'index.php?app=material&ctl=admin_material_basic&act=getMaterial'+'&'+data_string,
        //data: data_string,
        onRequest:function(){

        },
        onComplete:function(rs){
          if(!rs)return;
          rs=JSON.decode(rs);
          init(rs);

        }
      }).send();
    }

    function init(rs){
      var tmparr=findProduct(rs,'bm_id');
      store.unshift.apply(store,tmparr.reverse());
      createProduct(store);
    }

    function findProduct(arr,PRIMARY){


      if(!store.length)return arr;
      store.each(function(a){
        arr.each(function(b){
          if(a[PRIMARY]==b[PRIMARY])arr.erase(b);
        });
      });
      return arr;
    }

    function delProduct(obj,arr){
      arr.each(function(d){obj.delData(d);});
    }

    $('material-delall-btn').addEvent('click',function(e){
      if(!pag||!pag.data)return;
      var delarr=[];
      pag.data.each(function(d){
        delarr.push(d['bm_id']);
      });
      if(confirm('确认删除全部物料吗？')){
        delProduct(pag,delarr);
        emptyData();
      }
    });


  });
})();

function material_object_callback(rs,handle){
  var p_node = handle.getParent('div');
  var html = '已选择了1个物料,'+"<a href='javascript:void(0);' onclick='material_selected_show();'>查看关联的物料.</a>";
  if ($defined($('hand-selected-product')))
  {
    $('hand-selected-product').setHTML(html);
  } else {
    var div = new Element('div',{'html':html,'id':'hand-selected-product'});
    div.injectAfter(p_node);
  }
}

function material_selected_show(){
  new Dialog('index.php?app=material&ctl=admin_material_sales&act=showMaterials',{
    ajaxoptions:{data:$('hand-selected-product').getPrevious('div'),method:'post'}
  });
}

</script>
